﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MongoDB.Driver;
using MongoDB.Bson;
using System.Threading;

namespace Silmoon.DNS.Monitoring.Data
{
    public class Mongo : IDisposable
    {
        public MongoServer server = null;
        MongoCollection monitorListCollection = null;
        public bool Connecting { get; set; }
        public MongoDatabase database = null;
        GBC g;

        public MongoServerState State
        {
            get
            {
                if (server == null) return MongoServerState.None;
                else return server.State;
            }
        }

        public Mongo(GBC _g)
        {
            g = _g;
            Connecting = false;
            server = MongoServer.Create(Connectionstring);
        }
        public void OpenMongoDBA()
        {
            OpenMongoDB();
        }
        public bool OpenMongoDB()
        {
            if (Connecting) return false;
            bool _try = true;
            Connecting = true;

            while (_try)
            {
                try
                {
                    server.Connect();
                    database = server.GetDatabase(g.configure.db_mongodb_monitor_database);
                    _try = false;
                    Connecting = false;
                    g.onOutputText("connected to database.", 1);
                }
                catch (Exception ex)
                {
                    g.onThreadException(this, new System.Threading.ThreadExceptionEventArgs(ex));
                    g.onOutputText("unable connect to database!!!", -1);
                    if (ex.ToString().Contains("Server is not a primary and SlaveOk is false"))
                        g.onOutputText("configure database error, database is slave, but server run as master!!!", -1);
                    else
                        g.onOutputText("retry connect to database in 5 secounds after.");
                    Thread.Sleep(5000);
                }
            }
            return true;
        }
        public MongoDatabase GetMongoDatabase()
        {
            return server.GetDatabase(g.configure.db_mongodb_monitor_database);
        }
        public string Connectionstring
        {
            get
            {
                MongoConnectionStringBuilder stringBuilder = new MongoConnectionStringBuilder();
                stringBuilder.Username = g.configure.db_mongodb_username;
                stringBuilder.Password = g.configure.db_mongodb_password;
                stringBuilder.Server = new MongoServerAddress(g.configure.db_mongodb_server);
                stringBuilder.DatabaseName = g.configure.db_mongodb_monitor_database;
                if (g.configure.db_mongodb_type == "slave")
                    stringBuilder.SlaveOk = true;
                return stringBuilder.ConnectionString;
            }
        }
        //public MongoCollection GetCollection(string collectionName)
        //{
        //    while (true)
        //    {
        //        try
        //        {
        //            if (server.State == MongoServerState.Disconnected) if (!OpenMongoDB()) return null;
        //            if (recordsCollection == null)
        //            {
        //                recordsCollection = database.GetCollection<BsonDocument>(collectionName);
        //            }
        //            return recordsCollection;
        //        }
        //        catch
        //        {
        //            if (server.State == MongoServerState.Disconnected) if (!OpenMongoDB()) return null;
        //        }
        //    }
        //}
        public MongoCollection GetMonitorListCollection()
        {
            while (true)
            {
                try
                {
                    if (server.State == MongoServerState.Disconnected) if (!OpenMongoDB()) return null;
                    if (monitorListCollection == null)
                    {
                        monitorListCollection = database.GetCollection<BsonDocument>("monitorlist");
                    }
                    return monitorListCollection;
                }
                catch
                {
                    if (server.State == MongoServerState.Disconnected) if (!OpenMongoDB()) return null;
                }
            }
        }
        #region IDisposable 成员

        public void Dispose()
        {
            g.onOutputText("close database connection.", 1);
            server.Disconnect();
        }

        #endregion
    }
}